<?php
function getgpc ($k,$t='R') {
	switch ($t) {
		case 'P':$var=$_POST;break;
		case 'G':$var=$_GET;break;
		case 'C':$var=$_COOKIE;break;
		case 'R':$var=$_REQUEST;break;
	}
	return isset($var[$k])?(is_array($var[$k])?$var[$k]:trim($var[$k])):NULL;
}
function randstr ($long) {
  $str=str_repeat('abcdefghijklimnopqrstuvwxyz0123456789',5);
  $str=str_shuffle($str);
  $str=substr($str,0,$long);
  return $str; 
}
function jd ($data) {
	$result=@json_decode($data,1);
	return $result;
}
function curl_get($url,$data='',$ua=1){
  	$cookie=$_ENV['_model']->get_cookie();
	$ch=curl_init($url);
	if ($ua==1) $header=array('User-Agent: Mozilla/5.0 (Linux; U; Android 4.2.1; zh-cn; Coolpad8190Q Build/JOP40D) AppleWebKit/534.24 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.24 T5/2.0 baidubrowser/4.5.20.0 (Baidu; P1 4.2.1)');
	elseif ($ua==2) $header=array('User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36','Connection: keep-alive','Referer: http://www.baidu.com/');
	if (isset($data['header'])) $header=@array_merge($header,$data['header']);
	if (isset($header)) curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
	curl_setopt($ch,CURLOPT_COOKIE,$cookie);
	if (isset($data['post'])) {
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data['post']);
	}
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_HEADER,0);
	$get_url=curl_exec($ch);
	@curl_close($ch);
	return $get_url;
}
//生成POST1的信息
function client_mkClient_post1 ($is_bduss=TRUE) {
	static $client_id='';
	if (empty($client_id)) { //随机_client_id
		$client_id=array();
		for ($i=1;$i<=28;$i++) $client_id[]=strtoupper(randstr(2));
		$client_id=join('-',$client_id);
	}
	$arr=array(
		'_client_id'=>$client_id,
		'_client_type'=>'4',
		'_client_version'=>'1.2.1.17',
		'_phone_imei'=>md5(randstr(16))
	);
	if ($is_bduss) {
		$BDUSS=$_ENV['_model']->get_bduss();
		$post1=array(
			'BDUSS'=>$BDUSS
		);
		$post1=array_merge($post1,$arr);
	} else {
		$post1=$arr;
	}
	return $post1;
}
//生成POST2的信息
function client_mkClient_post2 () {
	$post2=array(
		'net_type'=>'3',
		'tbs'=>$_ENV['_model']->get_tbs()
	);
	return $post2;
}
//生成客户端信息
function client_mkInfo ($data1=array(),$data2=array(),$is_sign=TRUE) { //默认自动签名
	$post1=client_mkClient_post1();
	$post2=client_mkClient_post2();
	$post1=array_merge($post1,$data1);
	$post2=array_merge($post2,$data2);
	$post=array_merge($post1,$post2);
	if ($is_sign) {
		$sign=client_sign($post);
		$post['sign']=$sign;
	}
	return $post;
}
//生成客户端签名
function client_sign ($post) {
	$sign_str='';
	foreach ($post as $k=>$v) $sign_str.=$k.'='.$v;
	$sign=strtoupper(md5($sign_str.'tiebaclient!!!'));
	return $sign;
}
?>